package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.CommDAO;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;

public class CheckOut extends JFrame {

	private CommDAO dao = new CommDAO();
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JButton btnNewButton;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel room_num;
	private JLabel floor;
	private JLabel label_6;
	private JLabel label_7;
	private JLabel label_8;
	private JLabel idcard;
	private JLabel name;
	private JLabel start_time;
	private JLabel label_9;
	private JTextField textField_4;
	private JButton btnNewButton_1;
	private JLabel label_10;
	private JLabel id;
	private JButton btnNewButton_2;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					CheckOut frame = new CheckOut();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public CheckOut() {
		setTitle("删除");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 553, 374);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("录入编号");
		label.setBounds(109, 13, 54, 15);
		contentPane.add(label);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(161, 10, 86, 21);
		contentPane.add(textField);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(309, 10, 96, 21);
		contentPane.add(textField_1);
		
		JLabel label_1 = new JLabel("身份证");
		label_1.setBounds(267, 13, 54, 15);
		contentPane.add(label_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(161, 38, 86, 21);
		contentPane.add(textField_2);
		
		JLabel label_2 = new JLabel("姓名");
		label_2.setBounds(109, 41, 54, 15);
		contentPane.add(label_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(309, 41, 96, 21);
		contentPane.add(textField_3);
		
		JLabel label_3 = new JLabel("门牌号");
		label_3.setBounds(267, 44, 54, 15);
		contentPane.add(label_3);
		
		btnNewButton = new JButton("查询");
		btnNewButton.setBounds(225, 71, 73, 23);
		contentPane.add(btnNewButton);
		
		label_4 = new JLabel("门牌号");
		label_4.setBounds(163, 125, 54, 15);
		contentPane.add(label_4);
		
		label_5 = new JLabel("录入楼层");
		label_5.setBounds(163, 150, 54, 15);
		contentPane.add(label_5);
		
		room_num = new JLabel("");
		room_num.setForeground(Color.RED);
		room_num.setBounds(235, 125, 86, 15);
		contentPane.add(room_num);
		
		floor = new JLabel("");
		floor.setForeground(Color.RED);
		floor.setBounds(235, 150, 86, 15);
		contentPane.add(floor);
		
		label_6 = new JLabel("身份证号码");
		label_6.setBounds(161, 172, 68, 15);
		contentPane.add(label_6);
		
		label_7 = new JLabel("姓名");
		label_7.setBounds(161, 197, 54, 15);
		contentPane.add(label_7);
		
		label_8 = new JLabel("录入时间");
		label_8.setBounds(161, 222, 54, 15);
		contentPane.add(label_8);
		
		idcard = new JLabel("");
		idcard.setForeground(Color.RED);
		idcard.setBounds(235, 172, 86, 15);
		contentPane.add(idcard);
		
		name = new JLabel("");
		name.setForeground(Color.RED);
		name.setBounds(225, 197, 86, 15);
		contentPane.add(name);
		
		start_time = new JLabel("");
		start_time.setForeground(Color.RED);
		start_time.setBounds(225, 222, 86, 15);
		contentPane.add(start_time);
		
		label_9 = new JLabel("删除时间");
		label_9.setBounds(161, 247, 54, 15);
		contentPane.add(label_9);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(225, 244, 96, 21);
		contentPane.add(textField_4);
		
		btnNewButton_1 = new JButton("删除");
		btnNewButton_1.setEnabled(false);
		btnNewButton_1.setBounds(200, 290, 73, 23);
		contentPane.add(btnNewButton_1);
		
		label_10 = new JLabel("录入编号");
		label_10.setBounds(163, 100, 54, 15);
		contentPane.add(label_10);
		
		id = new JLabel("");
		id.setForeground(Color.RED);
		id.setBounds(225, 100, 86, 15);
		contentPane.add(id);
		
		btnNewButton_2 = new JButton("关闭");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_2.setBounds(298, 290, 73, 23);
		contentPane.add(btnNewButton_2);
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				btnNewButton_1.setEnabled(false);
				String sql = "select * from record where id = '" + textField.getText() + "' or idcard = '" + textField_1.getText() + "'"
						+ " or name = '" + textField_2.getText() + "' or room_num = '" + textField_3.getText() + "'";
				System.out.println(sql);
				List<Map<String,Object>> select = dao.select(sql);
				if(select.size() > 0) {
					
					if(select.get(0).get("end_time").toString().equals("")){
						btnNewButton_1.setEnabled(true);
						id.setText(select.get(0).get("id").toString());
						idcard.setText(select.get(0).get("idcard").toString());
						name.setText(select.get(0).get("name").toString());
						room_num.setText(select.get(0).get("room_num").toString());
						floor.setText(select.get(0).get("floor").toString());
						start_time.setText(select.get(0).get("start_time").toString());
						btnNewButton_1.setEnabled(true);
					}
				}
			}
		});
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = "update record set end_time = '" + textField_4.getText() + "' where id = '" + id.getText() + "'";
				String sql1 = "update room set state = '未录入' where num = '" + room_num.getText() + "' and floor = '" + floor.getText() + "'";
				System.out.println(sql);
				System.out.println(sql1);
				dao.commOper(sql);
				dao.commOper(sql1);
			}
		});
	}

}
